<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2024/5/8
 * Time: 15:04
 */

namespace app\controller\admin;
use app\model\MaterialQuality AS model;
use app\model\ProcessingFee AS pf;
use app\model\MaterialPrice AS mp;
class MaterialQuality extends Base
{
    public function list(){
        $where = [];
        $post = input('post.');
        !empty($post['name']) && $where[] = ['name','like','%'.$post['name'].'%'];
        $list = model::where($where)
            ->with([
                'mp','pf'
            ])
            ->paginate(['page' => $this->page,'list_rows' => $this->size]);
        return $this->view('/admin/mq/list',['list' => $list]);
    }
    public function info(){
        $id = input('id');
        $pf = pf::select()->toArray();
        $mp = mp::select()->toArray();
        $info = model::find($id);
        if (empty($info['id']))$info = [];
        else $info = $info->toArray();
        if (empty($info['material_price_id']))$info['material_price_id'] = 0;
        if (empty($info['processing_fee_id']))$info['processing_fee_id'] = 0;
        return $this->view(
            'admin/mq/info',
            [
                'info' => $info,
                'id' => $id,
                'mp' => $mp,
                'pf' => $pf
            ]);
    }
    public function save(){
        $id = input('id');
        $data = input('post.data');
        if (!empty($id)){
            $info = model::find($id);
            $info->save($data);
        }else{
            model::create($data);
        }
        return $this->msg();
    }
    public function delete($id){
        $ids = input('ids');
        if (!empty($ids)){
            model::where('id','IN',$ids)->delete();
        }else {
            model::find($id)->delete();
        }
        return $this->msg();
    }
}